
/****************************************************************************************************
 * This .do file performs the following tasks:
 * 
 * 1. Imports raw beleifs data
 * 2. Labels variables and choices for survey versions
 * 3. Cleans data:
 *    - Drops unnecessary variables
 *    - Recodes variables and prepares them for analysis
 *    - Merges with religion identifier 
 *    - Saves for Analysis
 ****************************************************************************************************/

*-----------------------------------
 * Setup Environment
 *-----------------------------------
set more off
clear all
set segmentsize 3g  /* Setting memory */

*-----------------------------------
 * Importing Raw Baseline Data
 *-----------------------------------


* Load the raw data file
use "$Data/Original/beliefs_raw.dta", clear



drop startdate enddate recordeddate locationlatitude ///
	locationlongitude distributionchannel userlanguage status ///
	responseid recipientlastname recipientfirstname recipientemail 

foreach var of varlist _all {
    capture confirm string variable `var'
    if (_rc == 7) continue
	*di "`var'"
    replace `var' = trim(itrim(`var'))
}

rename qid74 respondent_name
rename qid102 surveyor_name
rename qid102_6_text surveyor_name_other

destring durationinseconds, replace
g duration_mins = durationinseconds/60
move duration_mins durationinseconds
drop durationinseconds
la var duration_mins "Beliefs survey duration (mins)"

ren q115 slot
replace slot="1" if slot=="Morning"
replace slot="2" if slot=="Afternoon"
destring slot, replace

ren time current_date
ren q114 current_time
drop q112 // drop religion, will get this from B1
assert mi(qid99) & mi(qid100) & mi(qid21)
drop qid99 qid100 qid21
assert q124==id if !mi(id) & !mi(q124)
assert !mi(id)
drop q124


** Need to corect some ids
replace id = "KKAGT" if id == "KKAQT" 
replace id = "MGGBM" if id == "MGGMB" 
replace id = "KIINC" if id == "MOHST" 
replace id = "PBRUS" if id == "PBRVS" 
replace id = "QHURE" if id == "QHVRE" 
replace id = "UOMRH" if id == "RLHUA" 
replace id = "RPUGI" if id == "RPUEG" 
replace id = "CDWLN" if id == "CWDLN" 
replace id = "UXYPF" if id == "UXYPE" 
replace id = "XRCEW" if id == "XRCEW " 

isid id

ren v30 beliefmm
assert !mi(beliefmm)
la var beliefmm "Guess which of two Muslims made more bags per hour"
ren q113 guessmmgap 
la var guessmmgap "How much more?"
assert !mi(guessmmgap) if beliefmm!="They made the same number"

forv i=1/4 {
	assert !mi(belief`i')
	encode belief`i', gen(guess`i')
	la var guess`i' "Guess who made more per hour (guess `i')"		
	g guess`i'H = guess`i'==1
	replace guess`i'H = 0.5 if belief`i'=="They made the same number"
	la var guess`i'H "=1 if guessed Hindu made more,=0.5 if guessed same,=0 if guessed Muslim made more"
	g guess`i'M = 1 - guess`i'H
	la var guess`i'M "=1 if guessed Muslim made more,=0.5 if guessed same,=0 if guessed Hindu made more"
	order guess`i' guess`i'H guess`i'M, after(belief`i')
	drop belief`i'
}

ren guess_15 guess1gap
ren guess_26 guess2gap
ren guess_37 guess3gap
ren guess_48 guess4gap

forv i=1/4 {
	la var guess`i'gap "How much more did they make?"
	assert !mi(guess`i'gap) if inlist(guess`i'H,0,1)
	destring guess`i'gap, replace
	replace guess`i'gap=0 if mi(guess`i'gap) // if similarly productive
}

gen share_guessH = (guess1H + guess2H + guess3H + guess4H)/4
la var share_guessH "Fraction of guesses favouring Hindu"
gen share_guessM = 1-share_guessH
la var share_guessM "Fraction of guesses favouring Muslim"
assert inrange(share_guessH,0,1) & inrange(share_guessM,0,1)

order share_guessH share_guessM, after(guess4gap)

ren emoji happiness_today
assert !mi(happiness_today)
replace happiness_today="0" if happiness_today=="Very unhappy"
replace happiness_today="1" if happiness_today=="Somewhat unhappy"
replace happiness_today="2" if happiness_today=="Neither happy nor unhappy"
replace happiness_today="3" if happiness_today=="Somewhat happy"
replace happiness_today="4" if happiness_today=="Very happy"
destring happiness_today, replace
lab def happy 0 "Very unhappy" 1 "Somewhat unhappy" 2 "Neither happy nor unhappy" 3 "Somewhat happy" 4 "Very happy"
lab val happiness_today happy

drop partner_relationship // no usable data here, I think we dropped it and included it only in the mood survey

assert !mi(own_performance)
replace own_performance = substr(own_performance,1,2)
destring own_performance, replace
la var own_performance "Rate own performance today from 1 (very bad) to 10 (excellent)"

assert mi(gender) & total=="0" & inlist(total_new,"0","") & mi(name) & id1=="-100" ///
	& mi(religion) & id2=="-100" & mi(photo_url) & mi(id3) & mi(id4) & mi(id5)
drop gender total total_new name id1 religion id2 v53 photo_url id3 id4 id5

drop dic_payoff-diff78_ub date_str

forv i=1/8 {
	assert !mi(name`i')
	assert !mi(prod`i')
	destring prod`i', replace
}

destring pay, replace

order name7 name8, after(name6)

foreach var of varlist name? prod? {
	la var `var' "[preloaded for beliefs survey] `var'"
}

la var pay "[calculated within beliefs survey] participant pay"

foreach var of varlist _all {
	ren `var' be_`var'
}
ren be_id id

order id be_date
la var be_date "Date of beliefs survey (day 3 lead-in)"

** Remaining ID fixes needed for the match
replace id = "GEUTS" if id=="DIYIO"
replace id = "RNHUF" if id=="UOMRH"
drop if id=="HQUGE" /* couldn't find a match by name in baseline data */

tempfile beliefs
save `beliefs' 
 
 
** Now get baseline data
use $Data/Original/individualprod_ids_religion, clear
keep if full_participant==1
merge 1:1 id using `beliefs', assert(3) nogen


g be_bias_produc_ext = be_share_guessH if b1_hindu==1
replace be_bias_produc_ext = be_share_guessM if b1_hindu==0
la var be_bias_produc_ext "Fraction ingroup guessed more productive"

g be_bias_produc_int = (be_guess1gap*be_guess1H + ///
					be_guess2gap*be_guess2H + ///
					be_guess3gap*be_guess3H + ///
					be_guess4gap*be_guess4H - ///
					be_guess1gap*be_guess1M - ///
					be_guess2gap*be_guess2M - ///
					be_guess3gap*be_guess3M - ///
					be_guess4gap*be_guess4M)/4 if b1_hindu==1
replace be_bias_produc_int = (be_guess1gap*be_guess1M + ///
					be_guess2gap*be_guess2M + ///
					be_guess3gap*be_guess3M + ///
					be_guess4gap*be_guess4M - ///
					be_guess1gap*be_guess1H - ///
					be_guess2gap*be_guess2H - ///
					be_guess3gap*be_guess3H - ///
					be_guess4gap*be_guess4H)/4 if b1_hindu==0					
la var be_bias_produc_int "Avg. extra bags guessed produced by ingroup"

order be_bias_produc_ext be_bias_produc_int, after(be_share_guessM)

** Save
save $Data/Final/beliefs_cleaned, replace
